package com.leo.manage_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.manage_system.entity.UserWechat;
import com.leo.manage_system.entity.vo.UserVo;
import com.leo.manage_system.mapper.UserWechatMapper;
import com.leo.manage_system.service.UserWechatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 微信用户信息 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-17
 */
@Service
public class UserWechatServiceImpl extends ServiceImpl<UserWechatMapper, UserWechat> implements UserWechatService {
    @Autowired
    private UserWechatMapper userWechatMapper;


    @Override
    public Map<String,Object> getUserWechatPageQuery(Page<UserWechat> pageParam, UserVo UserVo) {
        QueryWrapper<UserWechat> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("user_id","res_type","name","picture","location","find_time");
        queryWrapper.orderByDesc("gmt_create");
        if( UserVo != null){
            if (StringUtils.isNotEmpty(UserVo.getOpenId())) {
                queryWrapper.eq("open_id", UserVo.getOpenId());
            }
//            if (StringUtils.isNotEmpty(UserVo.getDescription())) {
//                queryWrapper.like("description", UserVo.getDescription());
//            }
            if (StringUtils.isNotEmpty(UserVo.getNickName())) {
                queryWrapper.like("nick_name", UserVo.getNickName());
            }
        }

        userWechatMapper.selectPage(pageParam,queryWrapper);
//        System.out.println(pageParam.getRecords());
        List<UserWechat> records = pageParam.getRecords();

        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();

        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;

    }

    @Override
    public boolean deleteUserWechat(String openId) {
        try{
            userWechatMapper.deleteById(openId);
        }catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public List<Integer> getUserRegistrations() {
        Date date = new Date(); // this object contains the current date value
        date = getDate(date,1);
        List<Integer> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        for(int i = 0; i <= 7; i++){

            Date temp_date = getDate(date,-1);
            int temp = DateApart(date,temp_date);
            String string = "day"+ i;
            map.put(string,temp);
//            System.out.println(temp);
            date=temp_date;
            list.add(temp);
        }
        return list;
    }
    @Override
    public int getUserWechatTodayRegistrations() {
        Date date = new Date(); // this object contains the current date value
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(new Date());
        try {
            date = sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        Date date = new Date(); // this object contains the current date value
        date = getDate(date,1);
        Date temp_date = getDate(date,-1);
        return DateApart(date,temp_date);
    }

    @Override
    public List<Integer> getUserAccess() {
        Date date = new Date(); // this object contains the current date value
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(new Date());
        try {
            date = sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        Date date = new Date(); // this object contains the current date value
        date = getDate(date,1);
        List<Integer> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        for(int i = 0; i <= 7; i++){

            Date temp_date = getDate(date,-1);
            int temp = DateApart2(date,temp_date);
            String string = "day"+ i;
            map.put(string,temp);
            System.out.println(temp);
            date=temp_date;
            list.add(temp);
        }
        return list;
    }
    public int DateApart2(Date date1,Date date2){
        QueryWrapper<UserWechat> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("last_visit_time",date1);//小于
        queryWrapper.ge("last_visit_time",date2);//大于
        return userWechatMapper.selectCount(queryWrapper);
    }
    public Date getDate(Date date,int num){
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DATE,num);
        Date date2=rightNow.getTime();
        return date2;
    }
    public int DateApart(Date date1,Date date2){
        QueryWrapper<UserWechat> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("gmt_create",date1);//小于
        queryWrapper.ge("gmt_create",date2);//大于
        return userWechatMapper.selectCount(queryWrapper);
    }
}
